<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <917647288@qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 空模块，主要用于显示404页面，请不要删除
 */
class ApiController extends HomeController{
	protected function _initialize(){
		/* 读取站点配置 */
		$config = api('Config/lists');
        C($config); //添加配置
        if(!C('WEB_SITE_CLOSE')){
        	$this->error('站点已经关闭，请稍后访问~');
        }
        header('Access-Control-Allow-Origin:*');
    }

    public function cates(){

    }

    public function index(){
    	$map = I('get.');
    	$map['type'] = 1;
    	unset($map['id'], $map['p']);
    	$this->success('',$this->page('article', '*', $map, I('get.p')));
    }

    //查看文章详情
    public function detail($id){

    }

    /**
     * 后台分页
     * @ApiDescription(section="Admin", description="后台分页")
     * @ApiLazyRoute(uri="/admin/page",method="GET")
     * @ApiParams(name="table", type="string", nullable=false, description="table", cnname="表单名")
     * @ApiParams(name="field", type="string", nullable=false, description="field", cnname="字段")
     * @ApiParams(name="where", type="string", nullable=false, description="where", cnname="查询条件")
     * @ApiParams(name="p", type="int", nullable=false, description="field", cnname="当前页")
     * @ApiParams(name="size", type="int", nullable=false, description="field", cnname="每页记录数")
     * @ApiParams(name="system_key", type="string", nullable=false, description="system_ke", check="check_not_empty", cnname="系统key")
     * @ApiReturn(type="object", sample="{'code': 0,'message': 'success'}")
     */
    public function page($table, $field = '*' , $where = '1' ,$p = 1 ,$size = 10, $order ='`id` DESC'){
    	$model = D($table);
        $total = $model->where($where)->count(1);
        $p = intval($p);
        if(!$size)
            $size = 10;
        $size = intval($size);
        $totalPages = ceil($total / $size);
        $prevNum = ($p-1) * $size;
        if($prevNum < 0){
            $prevNum = 0;
        }
        $list = $model->field($field)->where($where)->order($order)->limit($prevNum, $size)->select();
        $lists = array();
        foreach ($list as $key => $value) {
        	$lists[$value['id']] = $value;
        }
        $return = array(
            'totalPages'=>$totalPages,
            'list'=>$lists
        );
        $return['total'] = $total;
        return $return;
    }

    public function success($info = '', $data = null){
    	$this->ajaxReturn(array('status'=>1,'data'=>$data, 'info'=>$info));
    }

    public function error($info = ''){
    	$this->ajaxReturn(array('status'=>0));
    }
}
